home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWShpLst.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  13.7 KB  |  480 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWShpLst.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9. //    The top shape is the first one in the list. The bottom shape is the last one. 
  10. //    FW_CShapeListShape draws the shape from the last (bottom) to the first (top)
  11.  
  12. #include "FWOS.hpp"
  13.  
  14. #ifndef FWSHPLST_H
  15. #include "FWShpLst.h"
  16. #endif
  17.  
  18. #ifndef FWSHAPE_H
  19. #include "FWShape.h"
  20. #endif
  21.  
  22. // ----- Foundation Includes -----
  23.  
  24. #ifndef FWSTREAM_H
  25. #include "FWStream.h"
  26. #endif
  27.  
  28. //========================================================================================
  29. //    RunTime Info
  30. //========================================================================================
  31.  
  32. #ifdef FW_BUILD_MAC
  33. #pragma segment FWGraphx_ShapeList
  34. #endif
  35.  
  36. //========================================================================================
  37. //    class FW_CShapeListRep
  38. //========================================================================================
  39.  
  40. #ifdef FW_USE_TEMPLATE_PRAGMAS
  41. #pragma template_access public
  42. #pragma template FW_TOrderedCollection<FW_CShape>
  43. #endif
  44.  
  45. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollection, FW_CShape)
  46. FW_DEFINE_AUTO(FW_CShapeListIterator)
  47. FW_DEFINE_AUTO(FW_CShapeListRep)
  48.  
  49. //----------------------------------------------------------------------------------------
  50. // FW_CShapeListRep::FW_CShapeListRep
  51. //----------------------------------------------------------------------------------------
  52.  
  53. FW_CShapeListRep::FW_CShapeListRep() :
  54.     fShapeList(NULL),
  55.     fValidAnchorPoint(FALSE)
  56. {
  57.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  58.     
  59.     FW_END_CONSTRUCTOR
  60. }
  61.  
  62. //----------------------------------------------------------------------------------------
  63. // FW_CShapeListRep::FW_CShapeListRep
  64. //----------------------------------------------------------------------------------------
  65.  
  66. FW_CShapeListRep::FW_CShapeListRep(const FW_CShapeListRep& other) :
  67.     fShapeList(NULL),
  68.     fValidAnchorPoint(FALSE)
  69. {
  70.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  71.  
  72.     FW_CShapeListIterator ite(other);
  73.     for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  74.         AdoptAtBottom(shape->Copy());
  75.     
  76.     FW_END_CONSTRUCTOR
  77. }
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // FW_CShapeListRep::FW_CShapeListRep
  81. //----------------------------------------------------------------------------------------
  82.  
  83. FW_CShapeListRep::FW_CShapeListRep(FW_CReadableStream& stream) :
  84.     fShapeList(NULL),
  85.     fValidAnchorPoint(FALSE)
  86. {
  87.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  88.     
  89.     unsigned long count;
  90.     stream >> count;
  91.     
  92.     while(count -- != 0)
  93.     {
  94.         FW_CShape* shape;
  95.         FW_READ_DYNAMIC_OBJECT(stream, &shape, FW_CShape);
  96.  
  97.         AdoptAtBottom(shape);
  98.     }
  99.     
  100.     FW_END_CONSTRUCTOR
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_CShapeListRep::~FW_CShapeListRep
  105. //----------------------------------------------------------------------------------------
  106.  
  107. FW_CShapeListRep::~FW_CShapeListRep()
  108. {
  109.     FW_START_DESTRUCTOR
  110.  
  111.     if (fShapeList)
  112.     {
  113.         FW_CShape* shape;
  114.         while ((shape = fShapeList->First()) != NULL)
  115.         {
  116.             Remove(shape);
  117.             delete shape;        
  118.         }
  119.         
  120.         delete fShapeList;
  121.         fShapeList = NULL;
  122.     }
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. // FW_CShapeListRep::GetCount
  127. //----------------------------------------------------------------------------------------
  128.  
  129. unsigned long FW_CShapeListRep::GetCount() const
  130. {
  131.     return fShapeList->Count();
  132. }
  133.  
  134. //----------------------------------------------------------------------------------------
  135. // FW_CShapeListRep::Purge
  136. //----------------------------------------------------------------------------------------
  137.  
  138. void FW_CShapeListRep::Purge()
  139. {
  140.     FW_CShapeListIterator ite(*this);
  141.     for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  142.     {
  143.         shape->Purge();
  144.     }
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. // FW_CShapeListRep::AdoptAtTop
  149. //----------------------------------------------------------------------------------------
  150.  
  151. void FW_CShapeListRep::AdoptAtTop(FW_CShape* shape)
  152. {
  153.     fShapeList->AddFirst(shape);
  154.     fValidAnchorPoint = FALSE;
  155. }
  156.  
  157. //----------------------------------------------------------------------------------------
  158. // FW_CShapeListRep::AdoptAtBottom
  159. //----------------------------------------------------------------------------------------
  160.  
  161. void FW_CShapeListRep::AdoptAtBottom(FW_CShape* shape)
  162. {
  163.     fShapeList->AddLast(shape);
  164.     fValidAnchorPoint = FALSE;
  165. }
  166.  
  167. //----------------------------------------------------------------------------------------
  168. // FW_CShapeListRep::AdoptBelow
  169. //----------------------------------------------------------------------------------------
  170.  
  171. void FW_CShapeListRep::AdoptBelow(FW_CShape* shapeToAdd, FW_CShape* belowWhich)
  172. {
  173.     fShapeList->AddAfter(belowWhich, shapeToAdd);
  174.     fValidAnchorPoint = FALSE;
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // FW_CShapeListRep::AdoptAbove
  179. //----------------------------------------------------------------------------------------
  180.  
  181. void FW_CShapeListRep::AdoptAbove(FW_CShape* shapeToAdd, FW_CShape* aboveWhich)
  182. {
  183.     fShapeList->AddBefore(aboveWhich, shapeToAdd);
  184.     fValidAnchorPoint = FALSE;
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. // FW_CShapeListRep::Contains
  189. //----------------------------------------------------------------------------------------
  190.  
  191. FW_Boolean FW_CShapeListRep::Contains(FW_CShape* shape) const
  192. {    
  193.     return fShapeList->Contains(shape);
  194. }
  195.  
  196. //----------------------------------------------------------------------------------------
  197. // FW_CShapeListRep::Remove
  198. //----------------------------------------------------------------------------------------
  199.  
  200. void FW_CShapeListRep::Remove(FW_CShape* shape)
  201. {
  202.     fShapeList->Remove(shape);
  203.     fValidAnchorPoint = FALSE;
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // FW_CShapeListRep::RemoveAll
  208. //----------------------------------------------------------------------------------------
  209.  
  210. void FW_CShapeListRep::RemoveAll()
  211. {
  212.     while (GetCount() != 0)
  213.         this->Remove(fShapeList->First());
  214. }
  215.  
  216. //----------------------------------------------------------------------------------------
  217. // FW_CShapeListRep::RemoveTop
  218. //----------------------------------------------------------------------------------------
  219.  
  220. FW_CShape* FW_CShapeListRep::RemoveTop()
  221. {
  222.     FW_ASSERT(GetCount() != 0);
  223.     fValidAnchorPoint = FALSE;
  224.     return fShapeList->RemoveFirst();
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. // FW_CShapeListRep::RemoveBottom
  229. //----------------------------------------------------------------------------------------
  230.  
  231. FW_CShape* FW_CShapeListRep::RemoveBottom()
  232. {
  233.     FW_ASSERT(GetCount() != 0);
  234.     fValidAnchorPoint = FALSE;
  235.     return fShapeList->RemoveLast();
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. // FW_CShapeListRep::MoveUp
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_Boolean FW_CShapeListRep::MoveUp(FW_CShape* shape)
  243. {
  244.     FW_CShape* before = fShapeList->Before(shape);
  245.     if (before != NULL)
  246.     {
  247.         fShapeList->Remove(shape);
  248.         fShapeList->AddBefore(before, shape);
  249.         return TRUE;
  250.     }
  251.  
  252.     return FALSE;
  253. }
  254.  
  255. //----------------------------------------------------------------------------------------
  256. // FW_CShapeListRep::MoveToTop
  257. //----------------------------------------------------------------------------------------
  258.  
  259. FW_Boolean FW_CShapeListRep::MoveToTop(FW_CShape* shape)
  260. {
  261.     if (fShapeList->Before(shape) != NULL)
  262.     {
  263.         fShapeList->Remove(shape);
  264.         fShapeList->AddFirst(shape);
  265.         return TRUE;
  266.     }
  267.  
  268.     return FALSE;
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. // FW_CShapeListRep::MoveDown
  273. //----------------------------------------------------------------------------------------
  274.  
  275. FW_Boolean FW_CShapeListRep::MoveDown(FW_CShape* shape)
  276. {
  277.     FW_CShape* after = fShapeList->After(shape);
  278.     if (after != NULL)
  279.     {
  280.         fShapeList->Remove(shape);
  281.         fShapeList->AddAfter(after, shape);
  282.         return TRUE;
  283.     }
  284.  
  285.     return FALSE;
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. // FW_CShapeListRep::MoveToBottom
  290. //----------------------------------------------------------------------------------------
  291.  
  292. FW_Boolean FW_CShapeListRep::MoveToBottom(FW_CShape* shape)
  293. {
  294.     if (fShapeList->After(shape) != NULL)
  295.     {
  296.         fShapeList->Remove(shape);
  297.         fShapeList->AddLast(shape);
  298.         return TRUE;
  299.     }
  300.  
  301.     return FALSE;
  302. }
  303.  
  304. //----------------------------------------------------------------------------------------
  305. // FW_CShapeListRep::Write
  306. //----------------------------------------------------------------------------------------
  307.  
  308. void FW_CShapeListRep::Write(FW_CWritableStream& stream) const
  309. {
  310.     unsigned long count = this->GetCount();
  311.     stream << count;
  312.     
  313.     FW_CShapeListIterator ite(*this);
  314.     for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  315.         FW_WRITE_DYNAMIC_OBJECT(stream, shape, FW_CShape);
  316. }
  317.  
  318. //----------------------------------------------------------------------------------------
  319. // FW_CShapeListRep::IsEqual
  320. //----------------------------------------------------------------------------------------
  321.  
  322. FW_Boolean FW_CShapeListRep::IsEqual(const FW_CShapeListRep* rep) const
  323. {
  324.     if (rep == this)
  325.         return TRUE;
  326.     
  327.     unsigned long count = this->GetCount();
  328.         
  329.     if(count == rep->GetCount())
  330.     {
  331.         FW_CShapeListIterator iteThis(*this);
  332.         FW_CShapeListIterator iteThat(*rep);
  333.         
  334.         FW_CShape* shapeThis = iteThis.First();
  335.         FW_CShape* shapeThat = iteThat.First();
  336.         
  337.         for (unsigned long i = 0; i< count; ++ i)
  338.         {
  339.             if(shapeThis != shapeThat)
  340.                 return FALSE;
  341.             
  342.             shapeThis = iteThis.Next();
  343.             shapeThat = iteThat.Next();    
  344.         }
  345.         return TRUE;
  346.     }
  347.  
  348.     return FALSE;
  349. }
  350.  
  351. //----------------------------------------------------------------------------------------
  352. //    FW_CShapeListRep::GetAnchorPoint
  353. //----------------------------------------------------------------------------------------
  354.  
  355. FW_CPoint FW_CShapeListRep::GetAnchorPoint() const
  356. {
  357.     if (fValidAnchorPoint)
  358.     {
  359.         return fAnchorPoint;
  360.     }
  361.     else
  362.     {
  363.         FW_Boolean first = TRUE;
  364.         FW_CPoint anchor, anAnchor;
  365.     
  366.         FW_CShapeListIterator ite(*this);
  367.         for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  368.         {
  369.             anAnchor = shape->GetAnchorPoint();
  370.             if (first)
  371.                 anchor = anAnchor;
  372.             else
  373.             {
  374.                 anchor.x = FW_Minimum(anchor.x, anAnchor.x);
  375.                 anchor.y = FW_Minimum(anchor.y, anAnchor.y);
  376.             }
  377.             first = FALSE;
  378.         }    
  379.     
  380.         ((FW_CShapeListRep*)this)->fAnchorPoint = anchor;
  381.         ((FW_CShapeListRep*)this)->fValidAnchorPoint = TRUE;
  382.         
  383.         return anchor;
  384.     }
  385. }
  386.  
  387. //========================================================================================
  388. //    class FW_PShapeList
  389. //========================================================================================
  390.  
  391. FW_DEFINE_AUTO_TEMPLATE(FW_TCountedPtr, FW_CShapeListRep)
  392.  
  393. #ifdef FW_USE_TEMPLATE_PRAGMAS
  394.  
  395. #pragma template_access public
  396. #pragma template FW_TCountedPtr<FW_CShapeListRep>
  397.  
  398. #else
  399.  
  400. template class FW_TCountedPtr<FW_CShapeListRep>;
  401.  
  402. #endif
  403.  
  404. //----------------------------------------------------------------------------------------
  405. // FW_PShapeList::FW_PShapeList
  406. //----------------------------------------------------------------------------------------
  407.  
  408. FW_PShapeList::FW_PShapeList() :
  409.     FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, ()))
  410. {
  411. }
  412.  
  413. //----------------------------------------------------------------------------------------
  414. // FW_PShapeList::FW_PShapeList
  415. //----------------------------------------------------------------------------------------
  416.  
  417. FW_PShapeList::FW_PShapeList(FW_CReadableStream& stream) :
  418.     FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, (stream)))
  419. {
  420. }
  421.  
  422. //----------------------------------------------------------------------------------------
  423. // FW_PShapeList::~FW_PShapeList
  424. //----------------------------------------------------------------------------------------
  425.  
  426. FW_PShapeList::~FW_PShapeList()
  427. {
  428. }
  429.  
  430. //----------------------------------------------------------------------------------------
  431. // FW_PShapeList::FW_PShapeList
  432. //----------------------------------------------------------------------------------------
  433.  
  434. FW_PShapeList::FW_PShapeList(const FW_PShapeList& other)
  435. {
  436.     SetRep(other.fRep);
  437. }
  438.  
  439. //----------------------------------------------------------------------------------------
  440. // FW_PShapeList::operator=
  441. //----------------------------------------------------------------------------------------
  442.  
  443. FW_PShapeList& FW_PShapeList::operator=(const FW_PShapeList& other)
  444. {
  445.     FW_TCountedPtr<FW_CShapeListRep>::operator=(other);
  446.     return *this;
  447. }
  448.  
  449. //----------------------------------------------------------------------------------------
  450. // FW_PShapeList::Copy
  451. //----------------------------------------------------------------------------------------
  452.  
  453. FW_PShapeList FW_PShapeList::Copy() const
  454. {
  455.     FW_PShapeList list;
  456.     list.SetRep(FW_NEW(FW_CShapeListRep, (*fRep)));
  457.     return list;
  458. }
  459.  
  460. //----------------------------------------------------------------------------------------
  461. //    operator <<
  462. //----------------------------------------------------------------------------------------
  463.  
  464. FW_CWritableStream& operator << (FW_CWritableStream& stream, const FW_PShapeList& list)
  465. {
  466.     list.fRep->Write(stream);
  467.     return stream;
  468. }
  469.  
  470. //----------------------------------------------------------------------------------------
  471. //    operator >>
  472. //----------------------------------------------------------------------------------------
  473.  
  474. FW_CReadableStream& operator >> (FW_CReadableStream& stream, FW_PShapeList& list)
  475. {
  476.     list.SetRep(FW_NEW(FW_CShapeListRep, (stream)));
  477.     return stream;
  478. }
  479.  
  480.